#include <iostream>
#include <vector>

using namespace std;

class Solution {
public:
    static int minOperations(vector<int>& nums, int x) {
        int sum = 0;//得到整个数组的总和
        for(auto i: nums)
        {
            sum += i;
        }
        int target = sum - x;//滑动窗口的目标值
        int len = -1;
        int s1 = 0;
        for(int left = 0, right = 0; right < nums.size(); right++)
        {
            //进窗口：s1 += nums[right]
            //判断：while(s1 > target) left++
            //出窗口：s1 -= nums[left]
            s1 += nums[right];
            while(s1 > target && left <= right)
                s1 -= nums[left++];
            if(s1 == target) //更新结果
            {
                len = max(len, right - left + 1);//说明有结果
            }
        }
        return len == -1 ? -1 : nums.size()-len;
    }
};

int main()
{
    // vector<int> vc = {1,1,4,2,3};
    // int x = 5;
    // vector<int> vc = {5,6,7,8,9};
    // int x = 4;
    vector<int> vc = {3,2,20,1,1,3};
    int x = 10;
    cout << Solution::minOperations(vc, x) << endl;
    return 0;
}